# coding: utf8



import os
import logging
import time
import re
import traceback
import requests_html
from multiprocessing.pool import ThreadPool


"""复旦大学
微电子学院
"""

logging.basicConfig(level=logging.INFO)

session = requests_html.HTMLSession()
pool = ThreadPool(os.cpu_count() * 2)  # 要使用 hanlp, 线程数设为1


reduce_space = re.compile(r' ')

sex_ptn = re.compile(r'([男|女])')
degree_ptn = re.compile(r'学位[:：](\S+)')
dep_ptn = re.compile(r'(?:所在单位|研究所)[:：](\S+)')
subj_ptn = re.compile(r'学科[:：](\S+)')

addr_ptn = re.compile(r'(?:通信地址|通讯地址|办公地址|办公地点|Address|地址)[:：]([ \S]+)')
tel_ptn = re.compile(r'(?:办公电话|电话|联系方式|Tel|tel)[:：]([0-9+\- ,]+)')
email_ptn = re.compile(r'(?:电子邮箱|电子信箱|E-mail|Email|email|e-mail|邮箱地址|邮\s*箱)[:：](\S+@[.\S]+)')
ri_ptn = re.compile(r'研究(?:方向|领域|重点)(?:为|包括)*[:：\n]*([ \n\S]+?\n{2,})')
pos_ptn = re.compile(r"(教授|副教授|助理教授|研究员|副研究员|博士导师|硕士导师|博士生研究生|硕士生导师|IEEE Fellow|IEEE\s*会员|博士|讲师|教师|高级工程师|工程师)")
edu_ptn = re.compile(r"教育背景[:：\n]*([ \S\n]+?(\n{2,}))")
split_ptn = re.compile(r'([;；。\n])')


def teacher_list():
    try:
        r = session.get("https://sme.fudan.edu.cn/info/list?channelcode=anzhicheng")
        if r.status_code == 200:
            lis = r.html.xpath("//div[contains(@class, 'teacher-list')][1]//a[@target='_blank']")
            teachers = []

            for li in lis:
                name = li.full_text.strip()
                name = reduce_space.sub('', name)
                url = list(li.absolute_links)[0]

                logging.info("name: %s, url: %s", name, url)
                teachers.append((name, url))
            pool.imap_unordered(teacher_detail, teachers)
        else:
            logging.warning("wrong status code: %s", r.status_code)
            r.raise_for_status()
    except Exception as e:
        traceback.print_exc()
        raise e


def teacher_detail(payload):
    name, url = payload
    logging.info("loading %s ...", name)

    try:
        r = session.get(url)
        if r.status_code == 200:
            container = r.html.xpath("//div[contains(@class,'teacher-info ')][1]")[0]
            avatar = container.xpath("//div[contains(@class, 'teacher-left')][1]/img/@src")
            avatar = r.html.base_url + avatar[0] if avatar else ''

            basic_info = "\n".join([p.full_text.strip() for p in container.xpath("//div[contains(@class,'teacher-left')][1]//p")])
            basic_info = reduce_space.sub('', basic_info)

            pos, tel, email, addr, dep = [ptn.search(basic_info) for ptn in (pos_ptn, tel_ptn, email_ptn, addr_ptn, dep_ptn)]
            pos, tel, email, addr, dep = [(match.group(1).strip() if match else '') for match in (pos, tel, email, addr, dep)]

            intro = "\n".join([p.full_text.strip() for p in container.xpath("//div[contains(@class,'teacher-right')][1]//p")])

            ri, edu = [ptn.search(intro) for ptn in (ri_ptn, edu_ptn)]
            ri, edu = [(match.group(1).strip() if match else '') for match in (ri, edu)]

            logging.info("name: %s, pos: %s, tel: %s, email: %s, addr: %s, dep: %s, edu: %s, ri: %s, avatar: %s", name, pos, tel, email, addr, dep, edu, ri, avatar)

            pass
        else:
            logging.warning("wrong status code: %s", r.status_code)
            r.raise_for_status()
    except Exception as e:
        traceback.print_exc()
        raise e


if __name__ == '__main__':
    teacher_list()

    pool.close()
    pool.join()

    session.close()

    pass




